package douban;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import edu.uci.ics.crawler4j.crawler.WebCrawler;
import uk.org.lidalia.slf4jext.Logger;
import uk.org.lidalia.slf4jext.LoggerFactory;

import java.io.IOException;

/**
 * Created by liuyaowen on 2016/8/26.
 */
public class UploadUtils {
    protected static final Logger logger = LoggerFactory.getLogger(UploadUtils.class);
   static String QINIU_URL = "http://*************.com/";


    //设置好账号的ACCESS_KEY和SECRET_KEY
   static String ACCESS_KEY = "*************";
   static String SECRET_KEY = "-*************";
     //要上传的空间
   static String bucketname = "*************";

    //密钥配置
    static Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
    //创建上传对象


    //简单上传，使用默认策略，只需要设置上传的空间名就可以了
    public static String getUpToken(){
        return auth.uploadToken(bucketname);
    }

    public static void upload(String imgName,String imgFileName) throws IOException{
        try {
            UploadManager uploadManager = new UploadManager();
            //调用put方法上传
            Response res = uploadManager.put(imgFileName, imgName, getUpToken());
            //打印返回的信息
        } catch (QiniuException e) {
            Response r = e.response;
            // 请求失败时打印的异常的信息
            System.out.println(r.toString());
            try {
                //响应的文本信息
                System.out.println(r.bodyString());
            } catch (QiniuException e1) {
                //ignore
            }
        }
    }

    public static void main(String args[]) throws IOException{
        UploadUtils.upload("201487113353.jpg","http://pic.5577.com/up/2014-8/201487113353.jpg");
    }

}


